package edu.flying.panda.taskmanager;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.TimePicker;

/**
 * Add Task Screen
 * 
 * @author FlyingPanda
 */

public class EditTaskScreen extends Activity {

	private EditText des;
	private EditText location;
	private EditText fullDes;
	private CheckBox completed;
	private String type;
	private int taskId;
	private Task task;
	
	
	private TextView tvDisplayTime;
	private TextView tvDisplayDate;
	private Button ChangeTime;
	private Button ChangeDate;
	private Button back_button;
	private Button addTask_button;
	private Button map_button;
	private Date due;
	
	
	private int hour;
	private int minute;
	private int day;
	private int month;
	private int year;
	
	static final int TIME_DIALOG_ID = 999;
	static final int DATE_DIALOG_ID = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_task_screen);

		Log.d("ETS", "1");
		due = new Date();
		des = ((EditText) findViewById(R.id.new_task_name));
		location = ((EditText) findViewById(R.id.new_task_loc));
		fullDes = ((EditText) findViewById(R.id.new_task_comments));
		completed = ((CheckBox) findViewById(R.id.task_completed));
		
		Log.d("ETS", "2");
		
		back_button = (Button) findViewById(R.id.new_task_back);
		addTask_button = (Button) findViewById(R.id.new_task_add);
		map_button = (Button) findViewById(R.id.new_task_map);
		
		addTask_button.setText(R.string.edit);
		
		Bundle extras = getIntent().getExtras();
		
		taskId = extras.getInt("task");
		
		task = TaskManagerActivity.DB.getTask(taskId);
		Log.d("ETS", "3");
		des.setText(task.getDescription());
		location.setText(task.getLocation());
		
		fullDes.setText(task.getDetailedDescription());
		completed.setChecked(task.isCompleted());
		Log.d("addTask","et5");
		if (task.getType().equals("personal")) {
			((RadioButton) findViewById(R.id.type_personal)).setChecked(true);
		} else if (task.getType().equals("work")) {
			((RadioButton) findViewById(R.id.type_work)).setChecked(true);
		} else {
			((RadioButton) findViewById(R.id.type_school)).setChecked(true);
		}
		Log.d("addTask","et6");
		Log.d("ETS", "4");
		setCurrentTaskOnView();
		Log.d("ETS", "4.5");
		addListenerOnButton();
		Log.d("ETS", "5");
		/**
		 * Listener for Back button, cancels Task creation and returns to Home
		 * Screen
		 */
		back_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), HomeScreen.class);
				startActivity(myIntent);
			}
		});
		
		
		/**
		 * Listener for Map button, goes to Map view
		 * Screen
		 */
		map_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), Map.class);
				myIntent.putExtra("location", location.getText().toString());
				startActivity(myIntent);
			}
		});
		

		/**
		 * Listener for addTask button, creates a new Task and returns to Home
		 * Screen
		 */
		addTask_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				TextView errorText = (TextView) findViewById(R.id.new_task_error);
				if (((RadioButton) findViewById(R.id.type_school)).isChecked()) {
					type = "school";
				} else if (((RadioButton) findViewById(R.id.type_personal))
						.isChecked()) {
					type = "personal";
				} else {
					type = "work";
				}

				if (!des.getText().toString().equals("")) {
					
					task.setDescription(des.getText().toString());
					task.setLocation(location.getText().toString());
					task.setDueDate(due);
					task.setDetailedDescription(fullDes.getText().toString());
					task.setType(type);
					task.setCompleted(completed.isChecked());
					
					TaskManagerActivity.DB.updateTask(task); 
							
					
					Intent myIntent = new Intent(v.getContext(),
							HomeScreen.class);
					startActivity(myIntent);

				} else {
					errorText.setText("Must enter a Task Name");
				}

			}
		});

	}//end onCreate
	
	
	
	// display current time
			public void setCurrentTaskOnView() {
		 
				tvDisplayTime = (TextView) findViewById(R.id.time_text);
				tvDisplayDate = (TextView) findViewById(R.id.day_text);
				
				Date d = task.getDueDate();
				hour = d.getHour();
				minute = d.getMinute();
				year = d.getYear();
				day = d.getDay();
				month = d.getMonth();
				
				// set current time into textview
				tvDisplayTime.setText(
		                    new StringBuilder().append(pad(hour))
		                                       .append(":").append(pad(minute)));
				
				tvDisplayDate.setText(
	                    new StringBuilder().append(pad(month+1))
	                                       .append("/").append(pad(day)).append("/").append(pad(year)));
				
				due.setHour(hour);
				due.setMinute(minute);
				due.setDay(day);
				due.setMonth(month);
				due.setYear(year);
				
		 
			}
		 
			public void addListenerOnButton() {
		 
				ChangeTime = (Button) findViewById(R.id.time_button);
				ChangeDate = (Button) findViewById(R.id.day_button);
				
				ChangeTime.setOnClickListener(new OnClickListener() {
		 
					
					@SuppressWarnings("deprecation")
					public void onClick(View v) {
		 
						showDialog(TIME_DIALOG_ID);
		 
					}
		 
				});
				
				ChangeDate.setOnClickListener(new OnClickListener() {
		 
					
					@SuppressWarnings("deprecation")
					public void onClick(View v) {
		 
						showDialog(DATE_DIALOG_ID);
		 
					}
		 
				});
		 
			}
		 
			@Override
			protected Dialog onCreateDialog(int id) {
				switch (id) {
				case TIME_DIALOG_ID:
					// set time picker as current time
					return new TimePickerDialog(this, 
		                                        timePickerListener, hour, minute,false);
				case DATE_DIALOG_ID:
					return new DatePickerDialog(this, 
							datePickerListener, year, month, day);
				}
				return null;
			}
		 
			private TimePickerDialog.OnTimeSetListener timePickerListener = 
		            new TimePickerDialog.OnTimeSetListener() {
				public void onTimeSet(TimePicker view, int selectedHour,
						int selectedMinute) {
					hour = selectedHour;
					minute = selectedMinute;
		 
					// set current time into textview
					tvDisplayTime.setText(new StringBuilder().append(pad(hour))
							.append(":").append(pad(minute)));
		 
					due.setHour(hour);
					due.setMinute(minute);
					
		 
				}
			};
		 
			private DatePickerDialog.OnDateSetListener datePickerListener =
		            new DatePickerDialog.OnDateSetListener() {

		                public void onDateSet(DatePicker view, int Myear, 
		                                      int monthOfYear, int dayOfMonth) {
		                    year = Myear;
		                    month = monthOfYear;
		                    day = dayOfMonth;

		                    tvDisplayDate.setText(new StringBuilder().append(pad(month+1))
	                                .append("/").append(pad(day)).append("/").append(pad(year)));
		                    
		                    
		        			due.setDay(day);
		        			due.setMonth(month);
		        			due.setYear(year);
		                    
		                }
		            };
			
			private static String pad(int c) {
				if (c >= 10)
				   return String.valueOf(c);
				else
				   return "0" + String.valueOf(c);
			}
}